import Vue from 'vue'
import VueRouter from 'vue-router'
const HomePage = () =>
    import ('views/homepage/HomePage')
const Demand = () =>
    import ('views/demand/Demand')
const Algorithm = () =>
    import ('views/algorithm/Algorithm')
const Login = () =>
    import ('views/login/Login')
const Register = () =>
    import ('views/register/Register')
const News = () =>
    import ('views/news/News')
const AlgoDetail = () =>
    import ('views/detail/AlgoDetail')
const DeDetail = () =>
    import ('views/detail/DeDetail')
const AlgoMain = () =>
    import ('views/algorithm/AlgoMain')
const DemandMain = () =>
    import ('views/demand/DemandMain')


Vue.use(VueRouter)

const routes = [{
        path: '',
        redirect: '/homePage'
    },
    {
        path: '/homePage',
        component: HomePage,
        meta: {
            title: '首页'
        }
    },
    {
        path: '/demand',
        component: Demand,
        meta: {
            title: '需求广场'
        },
        children: [{
            path: '',
            component: DemandMain,
            meta: {
                title: '需求广场'
            }
        }, {
            path: 'detail/:id',
            component: DeDetail,
            meta: {
                title: '需求详情'
            }
        }]
    },
    {
        path: '/algorithm',
        component: Algorithm,
        meta: {
            title: '算法广场'
        },
        children: [{
            path: '',
            component: AlgoMain,
            meta: {
                title: '算法广场'
            }
        }, {
            path: 'detail/:id',
            component: AlgoDetail,
            meta: {
                title: '硬件详情'
            }
        }]
    },
    {
        path: '/login',
        component: Login,
        meta: {
            title: '登录'
        }
    },
    {
        path: '/register',
        component: Register,
        meta: {
            title: '注册'
        }
    },
    {
        path: '/message',
        component: News,
        meta: {
            title: '消息'
        }
    }

]

const router = new VueRouter({
        mode: 'history',
        base: process.env.BASE_URL,
        routes
    })
    //导航守卫
router.beforeEach((to, from, next) => {
    //从 from 跳转到 to
    document.title = to.matched[0].meta.title;
    next();
})

const originalPush = VueRouter.prototype.push
VueRouter.prototype.push = function push(location) {
    return originalPush.call(this, location).catch(err => err)
}

export default router